/**
 * Title: ToastUtil.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-8-19 下午4:24:05
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Looper;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.service.utils.DataCheckUtil;
import com.luoxudong.phonenumber.service.utils.EvtLog;

/** 
 * ClassName: ToastUtil
 * Description:toast工具类
 * Create by 罗旭东
 * Date 2013-8-19 下午4:24:05
 */
public class ToastUtil {
private static final String TAG = "ToastUtil";
    
	public static void showToast(Context context, int toastId) {
		showToast(context, context.getString(toastId));
	}

	public static void showToast(final Context context, final String msg) {
		
		if(!isRunOnUiThread()) {
			if (context instanceof Activity)
			{
				((Activity)context).runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						showMsg(context, msg, Toast.LENGTH_SHORT);
					}
				});
			}
			/*Looper.prepareMainLooper();
			showMsg(context, msg, Toast.LENGTH_SHORT);
			Looper.loop();*/
		}
		else {
			showMsg(context, msg, Toast.LENGTH_SHORT);
		}
		
	}
	
	/**
	 * 
	 * @description:根据错误码转换成对应提示信息，有错误码时尽量使用该方法提示信息，过滤不必要提示的信息
	 * @return void
	 * @throws
	 */
	public static void showToast(Context context, int errorCode, String msg)
	{
		String errorMsg = msg;
		
		EvtLog.w(TAG, errorMsg);
		
		errorMsg = DataCheckUtil.errorCodeCheck(errorCode);
		
		if (errorMsg != null && !"".equals(errorMsg))
		{
			showToast(context, errorMsg);
		}
	}

	public static void showToastLongTime(Context context, String formatStr) {
		if(!isRunOnUiThread()) {
			Looper.prepare();
			showMsg(context, formatStr, Toast.LENGTH_LONG);
			Looper.loop();  
		}
		showMsg(context, formatStr, Toast.LENGTH_LONG);
	}
	
    public static boolean isRunOnUiThread() {
    	return Thread.currentThread().getId() == Looper.getMainLooper().getThread().getId();
    }
    
    private static void showMsg(Context context, String msg, int duration)
    {
    	if (msg == null || "".equals(msg))
    	{
    		return;
    	}
    	
    	WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

		int screenWidth = wm.getDefaultDisplay().getWidth();//屏幕宽度

		int screenHeight = wm.getDefaultDisplay().getHeight();//屏幕高度
		
    	Toast toast = new Toast(context);
    	TextView msgView = new TextView(context);
    	msgView.setTextColor(Color.WHITE);
    	msgView.setBackgroundResource(R.drawable.tip);
    	msgView.setPadding(10, 5, 10, 5);
    	msgView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    	msgView.setText(msg);
    	
		toast.setView(msgView);
		toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, screenHeight/6);
		toast.show();
    }
}
